import java.util.*;

class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
}

//class Person{
//    private String name = "Person";
//    int age=0;
//}
//public class project extends Person {
//    public String grade;
//
//    public static void main(String[] args) {
//        Person p = new project();
//        System.out.println(p);
//        int i=0;
////        throw Exception("som");
//    }
//
//
//}


 class project {
     public boolean checkValid(int[][] matrix) {
         int len=matrix.length;
         int[] arr=new int[len];
         for(int i=1;i<len+1;i++){
             arr[i-1]=i;
         }
         for(int[] row : matrix){
             int[] tmp=new int[len];
             System.arraycopy(row,0,tmp,0,len);
             Arrays.sort(tmp);
             for(int i=0;i<len;i++){
                 if(arr[i]!=tmp[i]){
                     return false;
                 }
             }
         }
         for(int i=0;i<len;i++){
             int[] tmp=new int[len];
             for(int j=0;j<len;j++){
                 tmp[j]=matrix[j][i];
             }
             Arrays.sort(tmp);
             for(int j=0;j<len;j++){
                 if(arr[j]!=tmp[j]){
                     return false;
                 }
             }
         }
         return true;
     }

















     public boolean containsNearbyDuplicate(int[] nums, int k) {
         Set<Integer> set=new HashSet<>();
         for(int i=0;i<nums.length;i++){
             if(set.contains(nums[i])){
                 return true;
             }
             set.add(nums[i]);
             if(set.size()>k){
                 set.remove(nums[i-k]);
             }
         }
         return false;
     }






















    public static void main(String[] args) {
        String str1 = "hello";
        String str2 = "he" + new String("1lo");
        System.out.println(str1 == str2);
    }

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head=new ListNode(-1);
        ListNode cur=head;
        int tmp=0;
        int sum=0;
        while (l1!=null||l2!=null){
            int x=l1==null?0:l1.val;
            int y=l2==null?0:l2.val;
            sum=x+y+tmp;
            tmp=sum/10;
            sum=sum%10;
            cur.next=new ListNode(sum);
            cur=cur.next;
            if(l1!=null){
                l1=l1.next;
            }
            if(l2!=null){
                l2=l2.next;
            }
        }
        if(tmp==1){
            cur.next=new ListNode(1);
        }
        return head.next;
    }














    public ListNode removeZeroSumSublists(ListNode head) {
        ListNode newHead=new ListNode(-1);
        newHead.next=head;
        ListNode prev=newHead;
        while (prev!=null){
            int sum=0;
            ListNode cur=prev.next;
            while (cur!=null){
                sum+=cur.val;
                if(sum==0){
                    prev.next=cur.next;
                    break;
                }else{
                    cur=cur.next;
                }
            }
            if(cur==null){
                prev=prev.next;
            }
        }
        return newHead.next;
    }


















    public ListNode removeDuplicateNodes(ListNode head) {
        ListNode cur=head;
        while (cur!=null){
            ListNode newCur=cur;
            while(newCur.next!=null){
                if(cur.val==newCur.next.val){
                    newCur.next=newCur.next.next;
                }else{
                    newCur=newCur.next;
                }
            }
            cur=cur.next;
        }
        return head;
    }




















    public int[] reversePrint(ListNode head) {
        ListNode cur=head;
        ListNode prev=null;
        int len=0;
        while (cur!=null){
            ListNode curNext=cur.next;
            cur.next=prev;
            prev=cur;
            cur=curNext;
            len++;
        }
        int[] arr=new int[len];
        int index=0;
        while (prev!=null){
            arr[index++]=prev.val;
            prev=prev.next;
        }
        return arr;
    }
}
